#!/bin/sh
#

F="configure-syberos"

if test "$*" = "--help" -o "$*" = "-h"; then
  echo "$F [--use-ndk-cflags] [OPTIONS]"
  echo ""
  echo "where:"
  echo "  --use-ndk-cflags Optional parameter to use the same compilation flags"
  echo "              	   as the one used by ndk-build. Android NDK r9 or later"   
  echo "                   is required when using this option."
  echo "  OPTIONS     	   Other options that will be passed directly to"
  echo "                   ./aconfigure script. Run ./aconfigure --help"
  echo "                   for more info."
  echo ""
  echo "Environment variables:"
  echo "  ANDROID_NDK_ROOT Specify the directory of Android NDK to use."
  echo "  APP_PLATFORM     Optionally specify the platform level used, e.g."
  echo "                   android-9. By default, configure will use the"
  echo "                   maximum platform level detected."
  echo "  TARGET_ABI       Optionally specify a single target architecture,"
  echo "                   e.g. armeabi-v7a, mips, x86. By default, the target"
  echo "                   architecture is armeabi. Only used when"
  echo "                   --use-ndk-cflags is specified."
  echo "  IGNORE_CFLAGS    Optionally specify compilation flags to be ignored."
  echo "                   Each grepped flag that satisfies the criteria will"
  echo "                   be ignored. Default:"
  echo "                   IGNORE_CFLAGS=\"\-M\|\-f*stack\|\-f*alias\""
  echo "                   Only used when --use-ndk-cflags is specified."
  echo ""
  exit 0
fi

# if test "x${ANDROID_NDK_ROOT}" = "x" || test ! -e ${ANDROID_NDK_ROOT}; then
#   echo "$F error: ANDROID_NDK_ROOT env var is not specified or invalid"
#   exit 0
# fi

# if test "x$APP_PLATFORM" = "x"; then
#   APP_PLATFORM=`ls ${ANDROID_NDK_ROOT}/platforms/ | sed 's/android-//' | sort -gr | head -1`
#   APP_PLATFORM="android-${APP_PLATFORM}"
#   echo "$F: APP_PLATFORM not specified, using ${APP_PLATFORM}"
# fi

# if test "x$TARGET_ABI" = "x"; then
#   TARGET_ABI="armeabi"
#   echo "$F: TARGET_ABI not specified, using ${TARGET_ABI}"
# fi

# if test "$TARGET_ABI" = "x86_64" || test "$TARGET_ABI" = "mips64"; then       
#     USR_LIB="/usr/lib64"
# else
#     USR_LIB="/usr/lib"
# fi


if test "$1" = "--use-ndk-cflags"; then
  shift
  ADD_CFLAGS="1"
  if test "x${IGNORE_CFLAGS}" = "x"; then
    IGNORE_CFLAGS="\-M\|\-f*stack\|\-f*alias"
  fi

  # NDK_OUT=`${ANDROID_NDK_ROOT}/ndk-build -n -C ${ANDROID_NDK_ROOT}/samples/hello-jni NDK_TOOLCHAIN_VERSION=4.9 APP_PLATFORM=${APP_PLATFORM} APP_ABI=${TARGET_ABI}`
  # if test ! "${NDK_OUT}"; then
  #   echo "$F error: failed to run ndk-build, check ANDROID_NDK_ROOT env var"
  #   exit 1
  # fi

  # for i in $NDK_OUT; do
  #   if test "x${NDK_CXX}" != "x" -a "$i" = "-o"; then break; fi

  #   # Parse NDK CXXFLAGS
  #   if test "x${NDK_CXX}" != "x" -a "x`echo $i|grep 'hello-jni'`" = "x"; then
  #     if test "x`echo $i|grep '\-\-sysroot='`" != "x"; then
  #       ANDROID_SYSROOT=`echo $i|sed 's/--sysroot=//'`;
  #     fi
  #     NDK_CXXFLAGS="${NDK_CXXFLAGS} $i"
  #   fi

  #   # Parse NDK CFLAGS
  #   if test "x${NDK_CC}" != "x" -a "x`echo $i|grep 'hello-jni'`" = "x" -a "${ADD_CFLAGS}" = "1"; then
  #     if test "$i" = "-c"; then ADD_CFLAGS="0"; else
  #       if test "x`echo $i|grep ${IGNORE_CFLAGS}`" = "x"; then
  #         NDK_CFLAGS="${NDK_CFLAGS} $i"
  #       fi
  #     fi
  #   fi

  #   # Find gcc toolchain
  #   if test "x${NDK_CC}" = "x" -a "x`echo $i | grep 'gcc'`" != "x"; then
  #     NDK_CC=$i
  #   fi
  #   # Find g++ toolchain
  #   if test "x`echo $i | grep 'g++'`" != "x"; then
  #     NDK_CXX=$i
  #   fi
  # done

  # # Get target host from NDK toolchain dir name
  # TARGET_HOST=`echo ${NDK_CC} | sed -e 's/.*\/toolchains\/\([^\/]*\).*/\1/'`
  
  # # Remove version number suffix (otherwise config.sub will return error, perhaps it just doesn't like the format)
  # TARGET_HOST=`echo ${TARGET_HOST} | sed -e 's/\-[0-9\.]*$//'`

  # # Make sure target host string has 'linux-android' in it
  # if test "x`echo ${TARGET_HOST} | grep 'linux-android'`" = "x"; then
  #   #TARGET_HOST=`echo ${TARGET_HOST} | sed -e 's/\(.*\)\-\([0-9\.]*\)/\1-linux-android-\2/'`
  #   TARGET_HOST="${TARGET_HOST}-linux-android"
  # fi
  # TARGET_HOST = aarch64-linux-android

  # TARGET_HOST = armv7tnhl-xuanwu-syberos

  # export CC="${NDK_CC}"
  # export CXX="${NDK_CXX}"
  # export AR=`echo ${NDK_CXX}|sed 's/-g++/-ar/'`;
  # export RANLIB=`echo ${NDK_CXX}|sed 's/-g++/-ranlib/'`;
  
  # export CC="/home/lxj/Syberos-Pdk/targets/target-i686-xuanwu/usr/bin/i586-meego-linux-gcc"
  # export CXX="/home/lxj/Syberos-Pdk/targets/target-i686-xuanwu/usr/bin/i586-meego-linux-g++"
  # export AR="/home/lxj/Syberos-Pdk/targets/target-i686-xuanwu/usr/bin/ar"
  # export RANLIB="/home/lxj/Syberos-Pdk/targets/target-i686-xuanwu/usr/bin/ranlib"
  
  export CC="/home/lxj/Syberos-Pdk/sdk/sdk-root/opt/cross/bin/armv7tnhl-meego-linux-gnueabi-gcc"
  export CXX="/home/lxj/Syberos-Pdk/sdk/sdk-root/opt/cross/bin/armv7tnhl-meego-linux-gnueabi-g++"
  export AR="/home/lxj/Syberos-Pdk/sdk/sdk-root/opt/cross/bin/armv7tnhl-meego-linux-gnueabi-ar"
  export RANLIB="/home/lxj/Syberos-Pdk/sdk/sdk-root/opt/cross/bin/armv7tnhl-meego-linux-gnueabi-ranlib"
  # export CC="/home/lxj/Syberos-Pdk/targets/target-i686-xuanwu/usr/bin/gcc"
  # export CXX="/home/lxj/Syberos-Pdk/targets/target-i686-xuanwu/usr/bin/g++"
  # export AR="/home/lxj/Syberos-Pdk/targets/target-i686-xuanwu/usr/bin/ar"
  # export RANLIB="/home/lxj/Syberos-Pdk/targets/target-i686-xuanwu/usr/bin/ranlib"
  # export CC="pdk exec_target-i686-xuanwu gcc"
  # export CXX="pdk exec_target-i686-xuanwu g++"
  
  

  # export LDFLAGS="${LDFLAGS} --sysroot=${ANDROID_SYSROOT} -fPIE -pie"
  # export LIBS="${LIBS} -lc -lgcc -ldl"
  # export CFLAGS="${NDK_CFLAGS} ${CFLAGS} -fPIE"
  # export CPPFLAGS="${CFLAGS} -fexceptions -frtti -fPIE"
  # export CXXFLAGS="${NDK_CXXFLAGS} -fexceptions -frtti -fPIE"
  # export LDFLAGS="--sysroot=/home/lxj/Syberos-Pdk/targets/target-i686-xuanwu -fPIE -pie -L/home/lxj/Syberos-Pdk/targets/target-i686-xuanwu/usr/lib/gcc/i586-meego-linux/4.6.4"
  
  export LDFLAGS="--sysroot=/home/lxj/Syberos-Pdk/sdk/sdk-root/opt/cross/armv7tnhl-meego-linux-gnueabi/include/c++/4.8.3/armv7tnhl-meego-linux-gnueabi -fPIE -pie -L/home/lxj/Syberos-Pdk/targets/target-armv7tnhl-xuanwu/usr/lib/gcc/armv7tnhl-meego-linux-gnueabi/4.6.4"
  
  export LIBS="-lc++ -lc -lgcc -ldl"
  # export CFLAGS="-fPIE -I/home/lxj/Syberos-Pdk/targets/target-armv7tnhl-xuanwu/usr/lib/gcc/i586-meego-linux/4.6.4/include -I/home/lxj/Syberos-Pdk/targets/target-i686-xuanwu/usr/include"
  # export CFLAGS="-fPIE -I/home/lxj/Syberos-Pdk/sdk/sdk-root/opt/cross/lib/gcc/armv7tnhl-meego-linux-gnueabi/4.8.3/include -I/home/lxj/Syberos-Pdk/sdk/sdk-root/usr/include"
  # export CFLAGS = "-pipe -g -Wall -W -D_REENTRANT -fPIC -DDEMO0213_LIBRARY -DQT_QML_DEBUG -DQT_CORE_LIB "
  export CPPFLAGS="${CFLAGS} -fexceptions -frtti -I/home/lxj/Syberos-Pdk/sdk/sdk-root/usr/lib -fPIE -I/home/lxj/Syberos-Pdk/sdk/sdk-root/usr/lib -I/home/lxj/Syberos-Pdk/sdk/sdk-root/usr/lib"
  export CXXFLAGS="-shared --sysroot=/home/lxj/Syberos-Pdk/targets/target-armv7tnhl-xuanwu -lgcc -no-canonical-prefixes -Wl,--build-id -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--warn-shared-textrel -Wl,--fatal-warnings -lc -lm -fexceptions -frtti -fPIE"
  
# /opt/cross/libexec/gcc/armv7tnhl-meego-linux-gnueabi/4.8.3/lto-wrapper

else

  if test "$TARGET_ABI" != "armeabi"; then
    echo "$F error: For targets other than 'armeabi', specify --use-ndk-cflags"
    exit 1
  fi
  
  TARGET_HOST="arm-linux-androideabi"
  
  ANDROID_TC_VER=`ls -d ${ANDROID_NDK_ROOT}/toolchains/${TARGET_HOST}-* | sed 's/clang/0/' | sort -gr | head -1`
  ANDROID_TC=`ls -d ${ANDROID_TC_VER}/prebuilt/* | grep -v gdbserver | head -1`
  if test ! -d ${ANDROID_TC}; then
    echo "$F error: unable to find directory ${ANDROID_TC} in Android NDK"
    exit 1
  fi

  export ANDROID_SYSROOT="${ANDROID_NDK_ROOT}/platforms/${APP_PLATFORM}/arch-arm"
  if test ! -d ${ANDROID_SYSROOT}; then
    echo "$F error: unable to find sysroot dir ${ANDROID_SYSROOT} in Android NDK"
    exit 1
  fi

  export CC="${ANDROID_TC}/bin/${TARGET_HOST}-gcc"
  export CXX="${ANDROID_TC}/bin/${TARGET_HOST}-g++"
  export AR="${ANDROID_TC}/bin/${TARGET_HOST}-ar"
  export RANLIB="${ANDROID_TC}/bin/${TARGET_HOST}-ranlib"

  export LDFLAGS="${LDFLAGS} --sysroot=${ANDROID_SYSROOT}"
  export LIBS="${LIBS} -lc -lgcc"
  export CFLAGS="${CFLAGS} --sysroot=${ANDROID_SYSROOT}"
  export CPPFLAGS="${CFLAGS} -fexceptions -frtti"
  export CXXFLAGS="${CXXFLAGS} -shared --sysroot=${ANDROID_SYSROOT} -fexceptions -frtti"

fi

if test "x${CC}" = "x" || test ! -e ${CC}; then
  echo "$F error: compiler not found, please check environment settings (TARGET_ABI, etc)"
  exit 1
fi

# C++ STL
# Note: STL for pjsua2 sample app is specified in pjsip-apps/src/swig/java/android/jni/Application.mk

# gnustl
#STDCPP_TC_VER=`ls -d ${ANDROID_NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/[0-9]* | sort -gr | head -1`
#STDCPP_CFLAGS="-I${STDCPP_TC_VER}/include -I${STDCPP_TC_VER}/libs/${TARGET_ABI}/include"
#STDCPP_LIBS="-lgnustl_static"
#STDCPP_LDFLAGS="-L${STDCPP_TC_VER}/libs/${TARGET_ABI}/"

# stlport
#STDCPP_CFLAGS="-I${ANDROID_NDK_ROOT}/sources/cxx-stl/stlport/stlport"
#STDCPP_LIBS="-lstlport_static -ldl"
#STDCPP_LDFLAGS="-L${ANDROID_NDK_ROOT}/sources/cxx-stl/stlport/libs/${TARGET_ABI}"

# libc++
# STDCPP_CFLAGS="-I${ANDROID_NDK_ROOT}/platforms/android-21/arch-arm64/usr/include"
# STDCPP_LIBS="-lc++"
# STDCPP_LDFLAGS="-L${ANDROID_NDK_ROOT}/platforms/android-21/arch-arm64/usr/lib"


# STDCPP_CFLAGS="-I${ANDROID_NDK_ROOT}/sources/cxx-stl/llvm-libc++/include -I${ANDROID_NDK_ROOT}/sources/android/support/include"
# STDCPP_LIBS="-lc++"
# STDCPP_LDFLAGS="-L${ANDROID_NDK_ROOT}/sources/cxx-stl/llvm-libc++/libs/${TARGET_ABI}"

# STDCPP_CFLAGS="-I/home/lxj/Syberos-Pdk/targets/target-i686-xuanwu/usr/lib/gcc/armv7tnhl-meego-linux-gnueabi/4.6.4/include -I/home/lxj/Syberos-Pdk/targets/target-i686-xuanwu/usr/include"
# STDCPP_LIBS="-lc++"
# STDCPP_LDFLAGS="-L${ANDROID_NDK_ROOT}/sources/cxx-stl/llvm-libc++/libs/${TARGET_ABI}"


# export CFLAGS="${CFLAGS} ${STDCPP_CFLAGS}"
# export LIBS="${STDCPP_LIBS} ${LIBS}"
# export LDFLAGS="${LDFLAGS} ${STDCPP_LDFLAGS}"

# Print settings
if test "1" = "1"; then
  echo "$F: calling ./configure with env vars:"
  echo " CC = ${CC}"
  echo " CXX = ${CXX}"
  echo " CFLAGS = ${CFLAGS}"
  echo " CXXFLAGS = ${CXXFLAGS}"
  echo " LDFLAGS = ${LDFLAGS}"
  echo " LIBS = ${LIBS}"
  echo " AR = ${AR}"
  echo " RANLIB = ${RANLIB}"
  echo " TARGET_HOST = ${TARGET_HOST}"
fi

./configure --host=${TARGET_HOST} $*
